package com.weetop.aklyscan.activity.qingdan;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.activity.MainActivity;
import com.weetop.aklyscan.base.BaseUrl;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import ZPL.ZPLPrinterHelper;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class ImgDialog1 extends Dialog {
    private Context mContext;
    private ImageView img;
    private TextView button, back;
    private String pic;

    public ImgDialog1(Context context, String pic) {
        super(context, R.style.promptDialog);
        setContentView(R.layout.dialog_img);
        mContext = context;
        this.pic = pic;
        initVar();
        initView();
        initAttr();
        initAction();
    }

    public void initVar() {
    }

    public void initView() {
        img = findViewById(R.id.img);
        button = findViewById(R.id.button);
        back = findViewById(R.id.back);
    }

    public void initAttr() {
        if(pic.contains("/public"))
            pic = pic.replace("/public","");
        LogUtils.e(pic);
        Glide.with(mContext).load(BaseUrl.url2 + pic).into(img);
    }

    public void initAction() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (ZPLPrinterHelper.IsOpened()) {
                        Bitmap bitmap = setImage(pic);
                        if (TextUtils.isEmpty(MainActivity.userInfoBean.getPrint_deviation_x())) {
                            ToastUtil.getInstance()._short(mContext, "水平偏移不能为空");
                            return;
                        }
                        if (TextUtils.isEmpty(MainActivity.userInfoBean.getPrint_deviation_y())) {
                            ToastUtil.getInstance()._short(mContext, "垂直偏移不能为空");
                            return;
                        }
                        MainActivity.mZPL.start();
                        MainActivity.mZPL.printData("^LL"+bitmap.getHeight()+"\r\n");
                        MainActivity.mZPL.PW(MainActivity.userInfoBean.getPrint_paper_width());
                        MainActivity.mZPL.printBitmap(MainActivity.userInfoBean.getPrint_deviation_x(), MainActivity.userInfoBean.getPrint_deviation_x(), bitmap);
                        int i = MainActivity.mZPL.end();
//                        if(i>0){
//                            ToastUtil.getInstance()._short(mContext, "打印成功");
//                        }else{
//                            ToastUtil.getInstance()._short(mContext, "打印失败");
//                        }
                    } else {
                        ToastUtil.getInstance()._short(mContext, "打印机未连接成功");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public static Bitmap setImage(String imageStr) {
        try {
            byte[] decode = Base64.decode(imageStr, Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(decode, 0, decode.length);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
    }
}
